﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.IO;

public partial class mobile_ajaxdata_Default : BaseDataPage
{
    BaseDataPage bp = new BaseDataPage();
    protected override void Page_Load(object sender, EventArgs e)
    {
        string aciton = Request.Params["aciton"];
        switch (aciton)
        {
            case "GetSaleByCompRpt":
                GetSaleByCompRpt();
                break;
            case "GetSaleByShopRpt":
                GetSaleByShopRpt();
                break;
            case "GetStockList":
                GetStockList();
                break;
            case "GetShopList":
                GetShopList();
                break;
            case "CheckRights":
                CheckRights();
                break;
            case "GetUserMenus":
                GetUserMenus();
                break;
            case "KM_DailyPayAmt_rpt":
                KM_DailyPayAmt_rpt();
                break;
            case "KM_ShopSaleMoneyDtl_MbRpt":
                KM_ShopSaleMoneyDtl_MbRpt();
                break;
            case "KM_ShopSaleMoneyDtl_Mb2Rpt":
                KM_ShopSaleMoneyDtl_Mb2Rpt();
                break;
            case "LoadData":
                LoadData();
                break;
            case "shoptvlist":
                shoptvlist();
                break;
            case "shoptvpagelist":
                shoptvpagelist();
                break;
            case "shopincount":
                shopincount();
                break;
            case "getmyamount":
                getmyamount();
                break;
            case "getshopstatement":
                getshopstatement();
                break;
            case "getshopdayamount":
                getshopdayamount();
                break;
            case "clpaycehck":
                clpaycehck();
                break;
            case "getmyshopsale":
                getmyshopsale();
                break;
            case "shoppayment":
                shoppayment();
                break;
        }
    }

    private void shoppayment()
    {
        string sbr = "";
        sbr = "{\"status\":\"123\",\"msg\":\"保存成功！\"}";
        Response.Write(sbr);
        Response.End();

        Response.ContentType = "text/plain";
        HttpPostedFile file = Request.Files["file"];
        string img = "file://" + Request.Params["path"]; //图片路径
        string userid = Request.Params["userid"];   //用户ID
        string shopid = Request.Params["shopid"];
        

        sbr = "{\"status\":\"" + file.FileName + "\",\"msg\":\"保存成功！\"}";
        Response.Write(sbr);
        Response.End();

        if (img != null && shopid != null && userid != null)
        {
            
            string sq = Path.GetFileNameWithoutExtension(img) + "zwh" + Path.GetExtension(img.TrimEnd(';'));

            string pathee = Server.MapPath("~/photo/");
            //sbr = "{\"status\":\"" + img + "\",\"msg\":\"保存成功！\"}";
            //Response.Write(sbr);
            //Response.End();
            string path = pathee + "\\" + sq;
            
            using (Stream s = File.OpenRead(img))
            {
                using (Stream s2 = File.OpenWrite(path))
                {
                    byte[] b = new byte[1024 * 4 * 4];
                    int j; while ((j = s.Read(b, 0, b.Length)) > 0)
                    {
                        s2.Write(b, 0, j);
                    }
                }
            }

            sbr = "{\"status\":\"1\",\"msg\":\"保存成功！\"}"; 

            //string sql = "";
            //if (mydb.DM_ExecuteNonQuery(sql) != -1)
            //{
            //    Response.Write("ok");
            //    Response.End();
            //}
            //else
            //{
            //    Response.Write("no");
            //    Response.End();
            //}
        }
        else
        {
            sbr = "{\"status\":\"0\",\"msg\":\"出现错误！\"}";
        }
        Response.Write(sbr);
        Response.End();
    }

    private void getmyshopsale()
    {
        string userid = Request.Params["userid"];
        string sql = @"select ShopCode,ShopName,cast(isnull(sum(a.PayAmount),0) as int) as daymoney from v_shop s
                    inner join Sys_UserDataRights b on b.SourceID = s.StockID
                    left join V_PosSale a on a.shopid = s.ShopID
                    and cast(convert(varchar(20),a.BillDate,23) as datetime) = cast(CONVERT(varchar(20),getdate(),23)as datetime) 
                    where b.UserID = " + userid+@"
                    group by ShopCode,ShopName
                    order by ShopCode";

        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            string sbr = "";
            sbr = "[";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sbr += "{\"daymoney\":\"" + dr["daymoney"]
                    + "\",\"ShopCode\":\"" + dr["ShopCode"]
                    + "\",\"ShopName\":\"" + dr["ShopName"]
                 + "\"},";
            }
            sbr = sbr.TrimEnd(',') + "]";
            Response.Write(sbr);
            Response.End();

        }
        else
        {
            Response.Write("NO_DATA");
            Response.End();
        }
    }

    private void clpaycehck()
    {
        string detail=Request.Params["detail"];
        string shopid = Request.Params["shopid"];
        string bdate = Request.Params["bdate"];
        if (mydb.DM_ExecuteNonQuery("update KM_Shops_StateMent set ClState =1,cldate=getdate(),remark='"+detail+"'  where shopcode='" + shopid + "' and ReconDate='"+bdate+"'") != -1)
        {
            Response.Write("OK");
            Response.End();
        }
        else
        {
            Response.Write("NO_ERR");
            Response.End();
        }
    }

    private void getshopdayamount()
    {
        string shopid = Request.Params["shopid"];
        string bdate = Request.Params["bdate"];

        mydb.DM_ExecuteNonQuery("update KM_Shops_StateMent set ClState =1,cldate=getdate(),remark=''" 
            + " where shopcode='" + shopid + "' and ReconDate='" + bdate + "'");

        string sql = @"select * from V_CustomerAccountDtl where shopcode='" + shopid + "' and saledate='" + bdate + "'";
        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            string sbr = "";
            sbr = "[";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sbr += "{\"ShopCode\":\"" + dr["ShopCode"]
                    + "\",\"ShopName\":\"" + dr["ShopName"]
                    + "\",\"saledate\":\"" + bdate
                    + "\",\"RealPayAmount\":\"" + dr["RealPayAmount"]
                    + "\",\"CashAmount\":\"" + dr["CashAmount"]
                    + "\",\"BankAmount\":\"" + dr["BankAmount"]
                    + "\",\"OtherAmount\":\"" + dr["OtherAmount"]
                    + "\",\"relfzamt\":\"" + (Convert.ToDouble(dr["relfzamt"]) + Convert.ToDouble(dr["relxamt"]))
                    //+ "\",\"relxamt\":\"" + dr["relxamt"]
                    + "\",\"relpjamt\":\"" + (Convert.ToDouble(dr["relpjamt"])+Convert.ToDouble(dr["rellpamt"]))
                    //+ "\",\"rellpamt\":\"" + dr["rellpamt"]
                    + "\",\"fcfzamt\":\"" + dr["fcfzamt"]
                    + "\",\"fcxamt\":\"" + dr["fcxamt"]
                    + "\",\"fcpjamt\":\"" + dr["fcpjamt"]
                    + "\",\"fclpamt\":\"" + dr["fclpamt"]
                    + "\",\"fctot\":\"" + dr["fctot"]
                    + "\",\"fyamt\":\"" + dr["fyamt"]
                    + "\",\"payclamt\":\"" + dr["payclamt"]
                    + "\",\"pay1\":\"" + dr["pay1"]
                    + "\",\"pay2\":\"" + dr["pay2"]
                    + "\",\"PeriAmount\":\"" + dr["PeriAmount"]
                    //+ "\",\"BgAmount\":\"" + dr["BgAmount"]
                    + "\",\"CurrAmount\":\"" + dr["sjpayclamt"]
                    + "\",\"CurrAmount2\":\"" + dr["sjpayclamt2"]
                    + "\",\"endAmount\":\"" + dr["endAmount"]
                    + "\",\"clstate\":\"" + dr["dzstate"]
                    + "\",\"remark\":\"" + dr["remark"]
                 + "\"},";
            }
            sbr = sbr.TrimEnd(',') + "]";
            Response.Write(sbr);
            Response.End();

        }
        else
        {
            Response.Write("NO_DATA");
            Response.End();
        }
    }

    private void getshopstatement()
    {
        string userid = Request.Params["userid"];
        string ClState = Request.Params["stype"];
        string pageindex = Request.Params["pageindex"];
        string pagecount = Request.Params["pagecount"];
        int showcount = Convert.ToInt32(pageindex) * Convert.ToInt32(pagecount);
        //修改为财务确认小票，加盟商才可以看到对账单
        string sql = @"select top " + pagecount + @" *
                        from 
                        (
                            select row_number() over(order by ReconDate desc) as rownumber,
                            a.ShopCode,s.ShopName,convert(varchar(20),ReconDate,23)as ReconDate,PeriAmount,CurrAmount from KM_Shops_StateMent a 
                            inner join v_shop s on s.ShopCode = a.ShopCode
                            inner join Sys_UserDataRights d on d.SourceID = s.StockID
                            where imgred =1 and ClState= " + ClState + " and d.UserID=" + userid + @"
                        ) A
                        where rownumber > " + showcount.ToString() ;

        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            string sbr = "";
            sbr = "[";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sbr += "{\"ShopCode\":\"" + dr["ShopCode"]
                    + "\",\"ShopName\":\"" + dr["ShopName"]
                    + "\",\"ReconDate\":\"" + dr["ReconDate"]
                    + "\",\"PeriAmount\":\"" + dr["PeriAmount"]
                    + "\",\"CurrAmount\":\"" + dr["CurrAmount"]
                 + "\"},";
            }
            sbr = sbr.TrimEnd(',') + "]";
            Response.Write(sbr);
            Response.End();

        }
        else
        {
            Response.Write("NO_DATA");
            Response.End();
        }
    }

    private void getmyamount()
    {
        string userid = Request.Params["userid"];
        string bdate = DateTime.Now.ToString("yyyy-MM-dd");
        string sql = @"select isnull(cast(sum(a.PayAmount) as int),0) as daymoney from V_PosSale a
                        inner join v_shop s on s.shopid = a.shopid
                         inner join Sys_UserDataRights b on b.SourceID = s.StockID
                    where cast(convert(varchar(20),a.BillDate,23) as datetime) = '" + bdate + "' and b.UserID = " + userid;
        string shopcount = mydb.DM_GetExecuteScalarObj("select count(*)as shopcount from Sys_UserDataRights where UserID="+userid).ToString();
        string dznum = mydb.DM_GetExecuteScalarObj(@"select count(*)as dznum from KM_Shops_StateMent a
                                                    inner join v_shop s on s.ShopCode = a.shopcode
                                                    inner join Sys_UserDataRights b on b.SourceID = s.StockID
                                                    where clstate = 0 and imgred = 1 and b.userid = " + userid).ToString();
        string dzmonthnum = mydb.DM_GetExecuteScalarObj(@"select count(*)as dznum from KM_CustomerMonthAcc a
                                                    inner join v_shop s on s.ShopCode = a.shopcode
                                                    inner join Sys_UserDataRights b on b.SourceID = s.StockID
                                                    where clstate = 0 and cwstate = 1 and b.userid = " + userid).ToString();
        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            string sbr = "";
            sbr = "[";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sbr += "{\"daymoney\":\"" + dr["daymoney"]
                    + "\",\"todate\":\"" + DateTime.Now.ToString("yyyy年MM月dd日")
                    + "\",\"shopcount\":\"" + shopcount
                    + "\",\"dzmonthnum\":\"" + dzmonthnum
                    + "\",\"dznum\":\"" + dznum
                 + "\"},";
            }
            sbr = sbr.TrimEnd(',') + "]";
            Response.Write(sbr);
            Response.End();

        }
        else
        {
            Response.Write("NO_DATA");
            Response.End();
        }
    }

    private void shopincount()
    {
        string sql = @"select * from (select count(*)as allcount from Bas_ShopEnTrans where isnull(finished,0) <> 1)as allcount,
                        (select count(*)as shopcount from Bas_ShopEnTrans where isnull(finished,0) <> 1 and startdate <>'1900-01-01 00:00:00.000') as shopcount,
                        (select count(*)as dincount from Bas_ShopEnTrans where startdate='1900-01-01 00:00:00.000')as dincount";
        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            string sbr = "";
            sbr = "[";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sbr += "{\"shopcount\":\"" + dr["shopcount"]
                    + "\",\"dincount\":\"" + dr["dincount"]
                    + "\",\"allcount\":\"" + dr["allcount"]
                 + "\"},";
            }
            sbr = sbr.TrimEnd(',') + "]";
            Response.Write(sbr);
            Response.End();

        }
        else
        {
            Response.Write("NO_DATA");
            Response.End();
        }
    }

    private void shoptvlist()
    {
        int i = 0;
        string sql = @"select * from Bas_ShopEnTrans where isnull(finished,0) <> 1 order by startdate ";
        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            string sbr = "";
            sbr = "[";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sbr += "{\"orderid\":\"" + (++i).ToString()
                 + "\",\"shopname\":\"" + dr["shopname"]
                 + "\",\"shoparea\":\"" + dr["shoparea"]
                 + "\",\"startdate\":\"" + (Convert.ToDateTime(dr["startdate"]).ToString("MM/dd HH:mm") == "01/01 00:00" ? "待定" : Convert.ToDateTime(dr["startdate"]).ToString("MM月dd日"))
                 + "\",\"enddate\":\"" + (Convert.ToDateTime(dr["enddate"]).ToString("MM/dd HH:mm") == "01/01 00:00" ? "待定" : Convert.ToDateTime(dr["enddate"]).ToString("MM月dd日HH:mm"))
                 + "\",\"remark\":\"" + dr["remark"]
                 + "\"},";
            }
            sbr = sbr.TrimEnd(',') + "]";
            Response.Write(sbr);
            Response.End();

        }
        else
        {
            Response.Write("NO_DATA");
            Response.End();
        }
    }

    private void shoptvpagelist()
    {
        int i = 0;
        string pageindex = Request.Params["pageindex"];
        string pagecount = Request.Params["pagecount"];
        int showcount = Convert.ToInt32(pageindex) * Convert.ToInt32(pagecount);

        string sql = @"select top " + pagecount + @" *
                        from 
                        (
                            select row_number() over(order by startdate desc) as rownumber,
                            orderid,shopname,shoparea,startdate,enddate,remark from Bas_ShopEnTrans
                            where isnull(finished,0) <> 1 and startdate <>'1900-01-01 00:00:00.000'
                        ) A
                        where rownumber > " + showcount.ToString();
        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            string sbr = "";
            sbr = "[";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sbr += "{\"orderid\":\"" + dr["rownumber"].ToString()
                 + "\",\"shopname\":\"" + dr["shopname"]
                 + "\",\"shoparea\":\"" + dr["shoparea"]
                 + "\",\"startdate\":\"" + (Convert.ToDateTime(dr["startdate"]).ToString("MM/dd HH:mm") == "01/01 00:00" ? "待定" : Convert.ToDateTime(dr["startdate"]).ToString("MM月dd日"))
                 + "\",\"enddate\":\"" + (Convert.ToDateTime(dr["enddate"]).ToString("MM/dd HH:mm") == "01/01 00:00" ? "待定" : Convert.ToDateTime(dr["enddate"]).ToString("MM月dd日HH:mm"))
                 + "\",\"remark\":\"" + dr["remark"]
                 + "\"},";
            }
            sbr = sbr.TrimEnd(',') + "]";
            Response.Write(sbr);
            Response.End();

        }
        else
        {
            Response.Write("NO_DATA");
            Response.End();
        }
    }

    private void KM_ShopSaleMoneyDtl_MbRpt()
    {
        string userid =Request.Params["userid"];
        string sdate = Request.Params["sdate"];
        string edate = Request.Params["edate"];
        string area = Request.Params["area"];
        string bigarea = Request.Params["bigarea"];
        string matkind = Request.Params["matkind"];
        string shopid = Request.Params["shopid"];
        if (shopid == "")
        {
            shopid = GetUserShopDataRights(userid);
            if (shopid == "")
                shopid = "没有任何权限";
        }
        string sql = "KM_ShopSaleMoneyDtl_MbRpt '" + sdate + "','" + edate + "','" + SplitStr(area) + "','" +
            SplitStr(bigarea) + "','" + SplitStr(shopid) + "','" + SplitStr(matkind) + "'";
        DataSet ds = db.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            string sbr = "";
            sbr = "[";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sbr += "{\"orderid\":\"" + dr["orderid"]
                 + "\",\"shopname\":\"" + dr["shopname"]
                 + "\",\"saleamt\":\"" + dr["saleamt"]
                 + "\",\"saleqty\":\"" + dr["saleqty"]
                 + "\",\"salecount\":\"" + dr["salecount"]
                 + "\",\"kdj\":\"" + dr["kdj"]
                 + "\",\"ldv\":\"" + dr["ldv"]
                 + "\"},";
            }
            sbr = sbr.TrimEnd(',') + "]";
            Response.Write(sbr);
            Response.End();

        }
        else
        {
            Response.Write("NO_DATA");
            Response.End();
        }
    }
    private void KM_ShopSaleMoneyDtl_Mb2Rpt()
    {
        string userid = Request.Params["userid"];
        string sdate = Request.Params["sdate"];
        string edate = Request.Params["edate"];
        string area = Request.Params["area"];
        string bigarea = Request.Params["bigarea"];
        string matkind = Request.Params["matkind"];
        string shopid = Request.Params["shopid"];
        string stype = Request.Params["stype"];
        if (shopid == "")
        {
            shopid = GetUserShopDataRights(userid);
            if (shopid == "")
                shopid = "没有任何权限";
        }
        string sql = "KM_ShopSaleMoneyDtl_Mb2Rpt '" + sdate + "','" + edate + "','" + SplitStr(area) + "','" +
            SplitStr(bigarea) + "','" + SplitStr(shopid) + "','" + SplitStr(matkind) + "',"+stype;
        DataSet ds = db.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            string sbr = "";
            sbr = "[";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sbr += "{\"byear\":\"" + dr["byear"]
                + "\",\"rownumber\":\"" + dr["rownumber"]
                 + "\",\"bdate\":\"" + dr["bdate"]
                 + "\",\"shopname\":\"" + dr["shopname"]
                 + "\",\"saleamt\":\"" + dr["saleamt"]
                 + "\",\"saleqty\":\"" + dr["saleqty"]
                 + ((stype == "0")?"":"\",\"saledate\":\"" + dr["saledate"])
                 + "\"},";
            }
            sbr = sbr.TrimEnd(',') + "]";

            bp.InsertActionLog(userid, "3111", "浏览", DateTime.Now.ToString());
            Response.Write(sbr);
            Response.End();

        }
        else
        {
            Response.Write("NO_DATA");
            Response.End();
        }
    }
    private string GetUserShopDataRights(string userid)
    {
        string reslt = "";
        string sql = @"select s.shopid as id,s.shopname as name from v_shop s 
                    inner join V_UserShopDataRights d on d.shopid  = s.shopid 
                        where userid=" + userid + " order by s.shopname";
        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                reslt += dr["id"].ToString()+",";
            }
        }
        return reslt.TrimEnd(',');
    }

    private void LoadData()
    {
        string actname = Request.Params["actname"];
        string userid = Request.Params["userid"];
        string area = Request.Params["area"];
        string bigarea = Request.Params["bigarea"];
        if(area !="")
        area = SplitStr(area).Replace("''","'");
        if(bigarea !="")
        bigarea = SplitStr(bigarea).Replace("''", "'"); ;

        string sql = "";
        switch (actname)
        {
            case "LoadArea":
                sql = @"select distinct OperationAreaID as id,OperationAreaName as name from V_UserShopDataRights  where userid=" + userid + "";
                break;
            case "LoadBigArea":
                sql = @"select distinct AreaID as id,AreaName as name from V_UserShopDataRights  
                    where userid=" + userid + " and isnull(areaid,'') <> '' " 
                                   + (area != "" ? " and  OperationAreaID in " + area : "") 
                                   + " order by areaname";
                break;
            case "LoadMatkind":
                sql = @"select distinct kindid as id,kindname as name from v_mat";
                break;
        }
        DataSet ds;
        if (actname == "LoadShop")
        {
            sql = @"select s.shopid as id,s.shopname as name from v_shop s 
                    inner join V_UserShopDataRights d on d.shopid  = s.shopid 
                        where userid=" + userid
                                       + (area != "" ? " and s.OperationAreaID in " + area : "")
                                       + (bigarea != "" ? " and s.AreaID in " + bigarea : "") 
                                       + " order by s.shopname";
            ds = mydb.DM_GetDateSet(sql);
        }
        else
        {
            ds = mydb.DM_GetDateSet(sql);
        }
        if (ds != null)
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                string sbr = "";
                sbr = "[";
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    sbr += "{\"id\":\"" + dr["id"]
                     + "\",\"name\":\"" + dr["name"]
                     + "\"},";
                }
                sbr = sbr.TrimEnd(',') + "]";
                Response.Write(sbr);
                Response.End();

            }
            else
            {
                Response.Write("NO_DATA");
                Response.End();
            }
        }
        else
        {
            Response.Write("NO_DATA");
            Response.End();
        }
    }

    private void GetShopList()
    {
        string userid = Request.Params["userid"];
        string sql = @"select s.* from v_shop s 
                    inner join V_UserShopDataRights d on d.shopid  = s.shopid 
                        where userid=" + userid + " order by s.shopname";
        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            string sbr = "";
            sbr = "[";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sbr += "{\"shopid\":\"" + dr["shopid"]
                 + "\",\"shopname\":\"" + dr["shopname"]
                 + "\"},";
            }
            sbr = sbr.TrimEnd(',') + "]";
            Response.Write(sbr);
            Response.End();

        }
        else
        {
            Response.Write("NO_DATA");
            Response.End();
        }
    }

    private void KM_DailyPayAmt_rpt()
    {
        string sdate = Request.Params["sdate"];
        string shopid = Request.Params["shopid"];
        if (shopid == "0")
        {
            shopid = "";
        }
        else
        {
            shopid = "(''" + shopid + "'')";
        }
        string sql = "KM_DailyPayAmt_rpt '" + sdate + "','" + shopid + "','orderid'";
        DataSet ds = db.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            string sbr = "";
            sbr = "[";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sbr += "{\"orderid\":\"" + dr["orderid"]
                 + "\",\"shopname\":\"" + dr["shopname"]
                 + "\",\"payamt\":\"" + dr["payamt"]
                 + "\",\"upayamt\":\"" + dr["upayamt"]
                 + "\",\"kdjrate\":\"" + dr["kdjrate"]
                 + "\",\"kdj\":\"" + dr["kdj"]
                 + "\",\"allpayamt\":\"" + dr["allpayamt"]
                 + "\",\"targetmoney\":\"" + dr["targetmoney"]
                 + "\",\"wcl\":\"" + dr["wcl"]
                 + "\",\"mrate\":\"" + dr["mrate"]
                 + "\"},";
            }
            sbr = sbr.TrimEnd(',') + "]";
            Response.Write(sbr);
            Response.End();

        }
        else
        {
            Response.Write("NO_DATA");
            Response.End();
        }
    }

    private void GetSaleByShopRpt()
    {
        string stockid = Request.Params["stockid"];
        string barcode = Request.Params["barcode"];
        DataSet ds = db.DM_GetDateSet("exec KM_SaleByShopRpt '"+stockid+"','"+barcode+"'");
        if (ds.Tables[0].Rows.Count > 0)
        {
            string sbr = "";
            sbr = "[";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sbr += "{\"materialcode\":\"" + dr["materialcode"]
                 + "\",\"materialname\":\"" + dr["materialname"]
                 + "\",\"indate\":\"" + dr["indate"]
                 + "\",\"purqty\":\"" + dr["purqty"]
                 + "\",\"salqty\":\"" + dr["salqty"]
                 + "\",\"actualqty\":\"" + dr["actualqty"]
                 + "\",\"zkl\":\"" + dr["zkl"]
                 + "\"},";
            }
            sbr = sbr.TrimEnd(',') + "]";
            Response.Write(sbr);
            Response.End();

        }
        else
        {
            Response.Write("NO_DATA");
            Response.End();
        }
    }

    private void GetStockList()
    {
        string userid = Request.Params["userid"];
        string sql = @"select * from sys_userdatarights where stype = 0 and userid=" + userid + " order by sourcename";
        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds.Tables[0].Rows.Count > 0)
        {
            string sbr = "";
            sbr = "[";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sbr += "{\"stockid\":\"" + dr["sourceid"]
                 + "\",\"stockname\":\"" + dr["sourcename"]
                 + "\"},";
            }
            sbr = sbr.TrimEnd(',') + "]";
            Response.Write(sbr);
            Response.End();

        }
        else
        {
            Response.Write("NO_DATA");
            Response.End();
        }
    }

    //检查浏览权限
    private void CheckRights()
    {
        string modecode = Request.Params["modecode"];
        string userid = Request.Params["userid"];
        string isBrowse = mydb.DM_GetExecuteScalarObj(@"select M_Browse from sys_modulerights a inner join sys_modules b on b.mid = a.mid
        where modulecode='" + modecode
            + "' and userid=" + userid).ToString();
        if (string.IsNullOrEmpty(isBrowse) || isBrowse =="-1")
        {
            Response.Write("NO_RIGHT");
            Response.End();
        }
        else
        {
            if (isBrowse.ToLower() == "true")
            {
                Response.Write("OK");
                Response.End();
            }
            else
            {
                Response.Write("NO_RIGHT");
                Response.End();
            }
        }
    }
    //获取用户菜单
    private void GetUserMenus()
    {
        string sbr = "";
        string userid = Request.Params["userid"];
        string sql = @"select b.ModuleCode,b.ModuleName,WebUrl from sys_modulerights a inner join sys_modules b on b.mid = a.mid
	                    where a.userid = "+userid+" and a.M_Browse =1 and b.ParentId = 1025 and Block =1";
        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds != null)
        {
            if (ds.Tables[0].Rows.Count == 0)
            {
                sbr = "{\"status\":\"1\",\"msg\":\"没有数据！\"}";
            }
            else
            {
                sbr = DataSetToJson(ds);
            }
        }
        else
        {
            sbr = "{\"status\":\"0\",\"msg\":\"出现错误！\"}";
        }
        Response.Write(sbr);
        Response.End();
    }
    private void GetSaleByCompRpt()
    {
        string barcode = Request.Params["barcode"];
        DataSet ds = db.DM_GetDateSet("exec km_salebycomprpt'','" + barcode + "',1");
        if (ds.Tables[0].Rows.Count > 0)
        {
            string sbr = "";
            sbr = "[";
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                sbr += "{\"materialcode\":\"" + dr["materialcode"]
                 + "\",\"materialname\":\"" + dr["materialname"]
                 + "\",\"saldate\":\"" + dr["saldate"]
                 + "\",\"dcount\":\"" + dr["dcount"]
                 + "\",\"purqty\":\"" + dr["purqty"]
                 + "\",\"salqty\":\"" + dr["salqty"]
                 + "\",\"actualqty\":\"" + dr["actualqty"]
                 + "\",\"scount\":\"" + dr["dxs"]
                 + "\",\"xxl\":\"" + dr["xxl"]
                 + "\",\"zkl\":\"" + dr["zkl"]
                 + "\"},";
            }
            sbr = sbr.TrimEnd(',') + "]";
            Response.Write(sbr);
            Response.End();

        }
        else
        {
            Response.Write("NO_DATA");
            Response.End();
        }
    }
}